package pl.edu.agh.iosr.gamblingzone.adminpanel.action;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

import pl.edu.agh.iosr.gamblingzone.dao.BettingSlipDAO;
import pl.edu.agh.iosr.gamblingzone.dao.UserDAO;
import pl.edu.agh.iosr.gamblingzone.model.BettingSlip;
import pl.edu.agh.iosr.gamblingzone.model.User;
import pl.edu.agh.iosr.gamblingzone.utils.DAOFactory;

@Stateful
@Scope(ScopeType.SESSION)
@Name("usersManage")
public class UserManageAction implements UsersManage {

	/** The dao. */
	@In(required=true)
	DAOFactory dao;
	
	private List<User> all;
	private UserDAO userDAO;
	private BettingSlipDAO betSlipDAO;
	private User details;
	private List<BettingSlip> currentBettingSlips;
	
	@Logger
	private Log log;

	@Create
	public void init() {
		userDAO = dao.getUserDAO();
		betSlipDAO = dao.getSlipDAO();
		currentBettingSlips = new ArrayList<BettingSlip>();
	}
	
	@Remove
	public void remove() {
		// NOTHING LEFT TO DO
	}
	
	public void removeUser(User u) {
		userDAO.makeTransient(u);
	}

	public void blockUser(User u) {
		u.setBlocked(true);
	}

	public void unblockUser(User u) {
		u.setBlocked(false);
	}

	public User getDetails() {
		return details;
	}

	public void setDetails(User details) {
		this.details = details;
		betSlipDAO.setPageSize(100);
		currentBettingSlips = betSlipDAO.findAll(details, 0);
	}

	public List<BettingSlip> getCurrentBettingSlips() {
		return currentBettingSlips;
	}

	public void setCurrentBettingSlips(List<BettingSlip> currentBettingSlips) {
		this.currentBettingSlips = currentBettingSlips;
	}

	public List<User> getAll() {
		all = userDAO.findAll();
		return all;
	}

	public void cancelUser(User u) {
		u.setCancelled(true);
	}

	public void uncancelUser(User u) {
		u.setCancelled(false);
	}
}
